/**
 * 住院病床管理模块 bed_query.jsp
 */  

/**
 * 选择住院楼后显示对应楼层
 */
function viewfloor1(){
	var bid = $('#liveBuildID1').combobox('getValue');
	if(bid!=''){
		$('#liveFloor1').combobox({
			url:"to_QueryFloors",
			queryParams:{
				"bid":bid
			}
		});
	}
	dynamicqueryBed1();
}
function viewfloor2(){
	var bid = $('#liveBuildID2').combobox('getValue');
	if(bid!=''){
		$('#liveFloor2').combobox({
			url:"to_QueryFloors",
			queryParams:{
				"bid":bid
			}
		});
	}
	dynamicqueryBed2();
}

function dynamicqueryBed2(){
	var Office = $('#bed_query_office2').combobox('getValue');
	var SickroomType = $('#bed_query_type2').combobox('getValue');
	var BuildID = $('#liveBuildID2').combobox('getValue');
	var FloorID = $('#liveFloor2').combobox('getValue');
	var Sex = $('#bed_query_sex2').combobox('getValue');
	$('#bed_query_table2').datagrid({
		url:"to_QueryBedManyIf",
		queryParams:{
			"office":Office,
			"sickroomType":SickroomType,
			"buildID":BuildID,
			"floorID":FloorID,
			"sex":Sex,
			"status":"空闲"
		}		
	});
}


/**
 * 动态查询病床
 */
function dynamicqueryBed1(){
	var Office = $('#bed_query_office').combobox('getValue');
	var SickroomType = $('#bed_query_type').combobox('getValue');
	var BuildID = $('#liveBuildID1').combobox('getValue');
	var FloorID = $('#liveFloor1').combobox('getValue');
	var Sex = $('#bed_query_sex').combobox('getValue');
	var Status = $('#bed_query_status').combobox('getValue');
	$('#bed_query_table').datagrid({
		url:"to_QueryBedManyIf",
		queryParams:{
			"office":Office,
			"sickroomType":SickroomType,
			"buildID":BuildID,
			"floorID":FloorID,
			"sex":Sex,
			"status":Status
		}		
	});
}
/**
 * 查询全部病床
 */
function findAllbed(){
	$('#bed_query_office').combobox('setValue',null);
	$('#bed_query_type').combobox('setValue',null);
	$('#liveBuildID1').combobox('setValue',null);
	$('#liveFloor1').combobox('setValue',null);
	$('#bed_query_sex').combobox('setValue',null);
	$('#bed_query_status').combobox('setValue',null);
	dynamicqueryBed1();
}


/**
 * 新增病床jsp  sickroom_add.jsp
 */
//表单提交数据
function commit_addsickroom(){
	$("#sickroom_add").form({
		onSubmit: function(param){
			var rows = $("#bed_table").datagrid("getRows");
			for(var i = 0; i < rows.length; i ++){
				param['sickroomBedtable[' + i +'].bedName'] = rows[i].bedName;
				param['sickroomBedtable[' + i +'].bedStatus'] = rows[i].bedStatus;
				param['sickroomBedtable[' + i +'].bedNotes'] = rows[i].bedNotes;
			}
			return true;
		},
		success: function(v){
			alert("新增成功");
		}
	});
	$("#sickroom_add").submit();
}
/**
 * 打开病床交换窗口
 */
function openBedExchangeWindow(index){
	$("#bed_query_table").datagrid('selectRow',index);
	var row = $("#bed_query_table").datagrid('getSelected');
	if(row.BEDSTATUS!="空闲"){
		$.messager.alert('提示','非法操作，该床位暂时无法入住！','info');
	}else{
		$("#Exchange_HiddenbedID").val(row.BEDID);
		$('#Exchange_newBuildID').textbox('setValue', row.BNAME);//楼号
		$('#Exchange_newFloorID').textbox('setValue', row.FNAME);//楼层
		$('#Exchange_newSickoomID').textbox('setValue', row.SICKROOMNAME);//房间
		$('#Exchange_newBedID').textbox('setValue', row.BEDNAME + '号床');//床位
		$('#BedExchangeWindow').window('open');
	}
}
/**
 * 关闭病床交换窗口
 */
function closeExchangeBedForm(){
	BedExchangeWindow.dialog('destroy');
}
/**
 * 创建住院查询窗口
 */
var PatientSelectWndToExchange;
function createBedSelectWndToExchange(){
	PatientSelectWndToExchange =$('<div/>').window({
		title:'住院查询',
		href:'to_queryPatientByLike_forTally',
		width:950,
		height:500,
		modal:true,
		collapsible:false,
		minimizable:false,
		maximizable:false,
		resizable:false,
		iconCls:'icon-search',
		onClose:function(){
			$(this).window('destroy');
		},
		onLoad:function(){
		}
	});
	$("#LiveInSelect").window("options").SelectLiveIn = "toExchangeBed";
}

/**
 * 将选中病床信息读取出来回填到住院登记表单
 */
function returnInfoToExchangeBed() {

	var row = $("#livePatient_queryTable").datagrid("getSelected");
	$("#Exchange_LiveID").textbox('setValue', row.liveid8);
	$("#Exchange_LiveName").textbox('setValue', row.patientname8);

	$.getJSON('to_findBedInfoByLiveID',{  //查询原来病床
		"liveID":row.liveid8
	},function(data){
		
		$("#Exchange_HiddenOldbedID").val(data.BEDID);
		$('#Exchange_oldBuildID').textbox('setValue', data.BUILDNAME);//楼号
		$('#Exchange_oldFloorID').textbox('setValue', data.FLOORNAME);//楼层
		$('#Exchange_oldSickoomID').textbox('setValue', data.SICKROOMNAME);//房间
		$('#Exchange_oldBedID').textbox('setValue', data.BEDNAME + '号床');//床位
	});
	
	PatientSelectWndToExchange.window('destroy');
}
/**
 * 初始化病床交换ID
 */
function initExchangeBedID(){
	$.getJSON('to_createExchangeBedID',{},function(v){
		$("#Exchange_exchangeID").textbox('setValue',v);
	}
	);
}
/**
 * 提交换床表单
 */
function submitbedExchange_Form(){
	var id = $("#Exchange_LiveID").textbox("getValue");
	if(id==''){
		$.messager.alert('提示','请先填写完信息！','info');
	}else{
		$("#bedExchange_Form").form({
			success:function(v){
				if(v=true){
					$.messager.alert('提示','更换成功！','info');
					$('#bedExchange_Form').form('reset');
					$("#bed_query_table").datagrid('reload');
					$('#BedExchangeWindow').window('close');
				}else{
					$.messager.alert('提示','更换失败！','info');
				}
			}
		});
		$("#bedExchange_Form").submit();
	}
}
/**
 * 打扫病房
 */
function sweepBed(index){
	$("#bed_query_table").datagrid('selectRow',index);
	var row = $("#bed_query_table").datagrid('getSelected');
	if(row.BEDSTATUS!="待打扫"){
		$.messager.alert('提示','该病房无需打扫！','info');
	}else{
		$.getJSON('to_sweepBed',{
			"bid":row.BEDID
		},
		function(v){
			if(v=true){
				$("#bed_query_table").datagrid('reload');
			}else{
				$.messager.alert('提示','操作失败！','info');
			}
		});
	} 
}
/**
 * 锁定病房
 * @param index
 */
function lockBed(index){
	$("#bed_query_table").datagrid('selectRow',index);
	var row = $("#bed_query_table").datagrid('getSelected');
	var status;
	var info;
	if(row.BEDSTATUS=="已锁定"||row.BEDSTATUS=="空闲"){
		if(row.BEDSTATUS=="已锁定"){
			status="空闲";
			info = "你将对该病房进行解锁，是否继续?";
		}else{
			status="已锁定";
			info = "你将对该病房进行锁定，是否继续?";
		}
		$.messager.confirm('提示',info, function(r){
			if (r){
				$.getJSON('to_lockBed',{
					"bid":row.BEDID,
					"status":status
				},
				function(v){
					if(v=true){
						$("#bed_query_table").datagrid('reload');
					}else{
						$.messager.alert('提示','操作失败！','info');
					}
				});
			}
		});
	}else{
		$.messager.alert('提示','该状态下无法进行锁定！','info');
	} 
}
/**
 * 查看病人信息
 * @param index
 */
function bookPatientInfoFromBed(index){
	$("#bed_query_table").datagrid('selectRow',index);
	var row = $("#bed_query_table").datagrid('getSelected');
	if(row.BEDSTATUS!="已住"){
		$.messager.alert('提示','该床位没有住人，无法查看！','info');
	}else{
		$.getJSON('to_bookPatientInfoFromBed',{
			"bid":row.BEDID
		},function(v){
			$('#bedInfo_liveName').textbox('setValue',v.PATIENTNAME8);
			$('#bedInfo_docname').textbox('setValue',v.STAFFNAME);
			$('#bedInfo_liveType').textbox('setValue',v.LIVETYPENAME);
			$('#bedInfo_intime').textbox('setValue',v.LIVEDATE);
			$('#bedInfo_liveoffice').textbox('setValue',v.LIVEOFFICE4);
			$('#bedInfo_liveage').textbox('setValue',v.PATIENTAGE);
			$('#bedInfo_livestatus').textbox('setValue',v.LIVESTATUS);
		});
		$('#BookFrombedWindow').window('open');
	}
}








